/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2506.java
 * @Author : WangRuoyu
 * @Date 2022/12/23 16:48
 */

package leetcode;

import java.util.HashMap;
import java.util.HashSet;

public class Q2506 {
    public int similarPairs(String[] words) {
        int res = 0;
        HashMap<HashSet<Character>, Integer> map = new HashMap<>();

        for (String word : words) {
            HashSet<Character> wordset = new HashSet<>();
            for (int i = 0; i < word.length(); i++) {
                wordset.add(word.charAt(i));
            }
            map.put(wordset, map.getOrDefault(wordset, 0) + 1);
        }

        for (HashSet<Character> set : map.keySet()) {
            if (map.get(set) >= 2) {
                res += (map.get(set) * (map.get(set) - 1)) / 2;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Q2506 sl = new Q2506();
        String[] words = {"aba", "aabb", "abcd", "bac", "aabc"};
        System.out.println(sl.similarPairs(words));
    }
}